﻿using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace DjangoRESTFrameworkDAL.REST
{
    public interface IPatch
    {
        string Url { get; }
    }


    public static class PatchEx
    {
        /// <summary>
        ///     获取详细内容
        /// </summary>
        /// <typeparam name="T">序列化数据模型类型</typeparam>
        /// <param name="dal">DAL实例化对象</param>
        /// <returns>返回实例对象</returns>
        public static async Task<T> PatchAsync<T>(this T dal) where T : IPatch
        {
            var content = new StringContent(JsonConvert.SerializeObject(dal));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = await RestHttpClient.Instance.PatchWithPolicyAsync(dal.Url, content);
            var responseBody = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<T>(responseBody);
        }
    }
}